package ClassFrame;

import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Font;

public class Utils {

    public static String getTimeInFormat(int time) {
        if (time <= 0) {
            return ("00:00");
        }
        int hour, minute, second;
        if (time < 10) {
            return ("00:0" + time);
        } else if (time < 60) {
            return ("00:" + time);
        } else if (time >= 60 && time < 3600) {
            String timeT = null;
            minute = time / 60;
            second = time - minute * 60;
            if (minute < 10) {
                timeT = "0" + minute + ":";
            } else {
                timeT = minute + ":";
            }
            if (second < 10) {
                timeT += "0" + second;
            } else {
                timeT += second;
            }
            return timeT;
        } else if (time >= 3600) {
            String timeT = null;
            hour = time / 3600;
            minute = (time - hour * 3600) / 60;
            second = time - hour * 3600 - minute * 60;
            if (minute < 10) {
                timeT = hour + ":0" + minute + ":";
            } else {
                timeT = hour + ":" + minute + ":";
            }
            if (second < 10) {
                timeT += "0" + second;
            } else {
                timeT += second;
            }
            return timeT;
        }
        return null;
    }
    private static Random r = null;

    /**
     * Return a random number in range [start, end]
     * @param start
     * @param end
     * @return a random number
     */
//    public static int rand(int start, int end) {
//        if (r == null) {
//            r = new Random();
//        }
//        int range = Math.abs(end - start + 1);
//        long fraction = (long) (range * r.nextInt());
//        int ran = (int) (fraction + start);
//        return ran;
//    }
    public static int rand(int start, int end) {
        if (r == null) {
            r = new Random();
        }
        while (true) {
            int result = r.nextInt() % (end + 1);
            if (result >= start) {
                return result;
            }
        }
    }

    public static int getStringWidth(String input, Font font) {
        int result = 0;
        for (int i = 0; i < input.length(); i++) {
            result += font.charWidth(input.charAt(i));
        }
        return result;
    }

    public static int getStringHeigh(Font font) {
        return font.getHeight();
    }

    public static Vector splitText(String str, Font f, int printWidth, int maxRow) {
        Vector retV = new Vector();
        String ss = "";
        int tempSize = str.length();
        str = str + "   ";
        for (int i = 0; i < tempSize; i++) {
            if (maxRow > 0 && retV.size() == maxRow) {
                break; // get limited row
            }
            if (f.stringWidth(ss) < printWidth) {
                if ((int) str.charAt(i) == 10) { // enter key
                    retV.addElement(ss);
                    ss = "";
                } else {
                    ss = ss + str.charAt(i);
                    if (i == (tempSize - 1)) {
                        retV.addElement(ss);
                    }
                }
            } else {
                i--;
                int cc = 0;
                String dd = "";
                int tempSize2 = ss.length();
                for (int ee = 0; ee < tempSize2; ee++) {
                    if (ss.charAt(tempSize2 - 1 - ee) == ' ') {
                        cc = tempSize2 - 1 - ee;
                        break;
                    } else if (ee == tempSize2 - 1) {
                        for (int ee1 = 0; ee1 < tempSize2; ee1++) {
                            if ((ss.charAt(tempSize2 - 1 - ee1) == ':'
                                    || ss.charAt(tempSize2 - 1 - ee1) == ';'
                                    || ss.charAt(tempSize2 - 1 - ee1) == '/'
                                    || ss.charAt(tempSize2 - 1 - ee1) == '.'
                                    || ss.charAt(tempSize2 - 1 - ee1) == ',')) {
                                cc = tempSize2 - ee1;
                                break;
                            } else if (ee1 == tempSize2 - 1) {
                                cc = tempSize2 - 2;
                            }
                        }
                    }
                }
                dd = ss.substring(cc);
                ss = ss.substring(0, cc);
                retV.addElement(ss);
                ss = dd.trim();
            }
        }
        return retV;
    }

    public static Vector splitText(String str, CFont f, int printWidth, int maxRow) {
        Vector retV = new Vector();
        String ss = "";
        int tempSize = str.length();
        str = str + "   ";
        for (int i = 0; i < tempSize; i++) {
            if (maxRow > 0 && retV.size() == maxRow) {
                break; // get limited row
            }
            if (f.StringWidth(ss) < printWidth) {
                if ((int) str.charAt(i) == 10) { // enter key
                    retV.addElement(ss);
                    ss = "";
                } else {
                    ss = ss + str.charAt(i);
                    if (i == (tempSize - 1)) {
                        retV.addElement(ss);
                    }
                }
            } else {
                i--;
                int cc = 0;
                String dd = "";
                int tempSize2 = ss.length();
                for (int ee = 0; ee < tempSize2; ee++) {
                    if (ss.charAt(tempSize2 - 1 - ee) == ' ') {
                        cc = tempSize2 - 1 - ee;
                        break;
                    } else if (ee == tempSize2 - 1) {
                        for (int ee1 = 0; ee1 < tempSize2; ee1++) {
                            if ((ss.charAt(tempSize2 - 1 - ee1) == ':'
                                    || ss.charAt(tempSize2 - 1 - ee1) == ';'
                                    || ss.charAt(tempSize2 - 1 - ee1) == '/'
                                    || ss.charAt(tempSize2 - 1 - ee1) == '.'
                                    || ss.charAt(tempSize2 - 1 - ee1) == ',')) {
                                cc = tempSize2 - ee1;
                                break;
                            } else if (ee1 == tempSize2 - 1) {
                                cc = tempSize2 - 2;
                            }
                        }
                    }
                }
                dd = ss.substring(cc);
                ss = ss.substring(0, cc);
                retV.addElement(ss);
                ss = dd.trim();
            }
        }
        return retV;
    }

//	public static String normalizationQuestion(String str, Font font,
//			int printWidth, int maxRow) {
////		if (str.trim().equalsIgnoreCase("") || str == null)
////			return "";
//		Vector v = splitText(str, font, printWidth, maxRow);
////		Iterator iter = v.iterator();
//		String result = "";
//		while (iter.hasNext()) {
//			result = result + iter.next() + "\n";
//		}
//		return result;
//	}
    // Hàm cắt chuỗi theo token - trả về mảng String
    public static String[] split(String aStr, char token) {
        int startPos = 0;
        int strArrayLength = 0;
        int flag;
        for (int i = 0; i < aStr.length(); i++) {
            if (aStr.charAt(i) == token) {
                strArrayLength++;
            }
        }
        strArrayLength = strArrayLength + 1;
        String[] strArray = new String[strArrayLength];
        int index = 0;
        for (int i = 0; i < aStr.length(); i++) {
            flag = 0;
            if (aStr.charAt(i) == token) {
                int tokenPos = aStr.indexOf(token, startPos + 1);
                String temp = aStr.substring(startPos, tokenPos);
                if (flag == 0) {
                    strArray[index] = temp.trim();
                    index++;
                }
                startPos = tokenPos + 1;
            }
        }
        strArray[index] = aStr.substring(startPos, aStr.length());
        return strArray;
    }

    // Hàm cắt chuỗi theo token - trả về mảng int
    public static int[] splitInt(String aStr, char token) {
        String[] str = split(aStr, token);
        int[] arr = new int[str.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(str[i]);
        }
        return arr;
    }

    public static String replaceAll(String text, String searchString,
            String replacementString) {
        StringBuffer sBuffer = new StringBuffer();
        int pos = 0;
        while ((pos = text.indexOf(searchString)) != -1) {
            sBuffer.append(text.substring(0, pos) + replacementString);
            text = text.substring(pos + searchString.length());
        }
        sBuffer.append(text);
        return sBuffer.toString();
    }

    // Hàm chuyển chuỗi có dấu thành không dấu
    public static String signToUnsign(String input) {
        char[] tmp = input.toLowerCase().toCharArray();
        for (int i = 0; i < tmp.length; i++) {
            switch (tmp[i]) {
                case 'á':
                case 'à':
                case 'ạ':
                case 'ả':
                case 'ã':
                case 'â':
                case 'ấ':
                case 'ầ':
                case 'ậ':
                case 'ẩ':
                case 'ẫ':
                case 'ắ':
                case 'ă':
                case 'ằ':
                case 'ặ':
                case 'ẳ':
                case 'ẵ':
                case 'Ă':
                    tmp[i] = 'a';
                    break;
                case 'đ':
                case 'Đ':
                    tmp[i] = 'd';
                    break;
                case 'é':
                case 'è':
                case 'ẻ':
                case 'ẽ':
                case 'ẹ':
                case 'ê':
                case 'ế':
                case 'ề':
                case 'ể':
                case 'ễ':
                case 'ệ':
                    tmp[i] = 'e';
                    break;
                case 'ò':
                case 'ó':
                case 'õ':
                case 'ỏ':
                case 'ọ':
                case 'ô':
                case 'ố':
                case 'ồ':
                case 'ỗ':
                case 'ổ':
                case 'ộ':
                case 'ơ':
                case 'ờ':
                case 'ớ':
                case 'ở':
                case 'ỡ':
                case 'ợ':
                case 'Ổ':
                    tmp[i] = 'o';
                    break;
                case 'ý':
                case 'ỳ':
                case 'ỷ':
                case 'ỵ':
                case 'ỹ':
                    tmp[i] = 'y';
                    break;
                case 'í':
                case 'ì':
                case 'ỉ':
                case 'ĩ':
                case 'ị':
                    tmp[i] = 'i';
                    break;
                case 'ú':
                case 'ù':
                case 'ủ':
                case 'ũ':
                case 'ụ':
                case 'ư':
                case 'ứ':
                case 'ừ':
                case 'ự':
                case 'ử':
                case 'ữ':
                    tmp[i] = 'u';
                    break;
            }
        }
        return String.valueOf(tmp);
    }

    public static String segment(String input, int numCharInLine) {
        int count = 0;
        int length = input.length();
        for (int i = 0; i < length; i++) {
            if (i - count >= numCharInLine && input.charAt(i) == ' ') {
                count = i;
                input = input.substring(0, i) + "\n" + input.substring(i, input.length());
                length++;
            }
        }
        return input;
    }
}
